<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>旅游攻略表</title>
    <!-- 引入element-ui样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css">
    <!--    上传图片样式-->
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>

</head>
<body>
<div id="app">

    <el-container style="height: 800px; border: 1px solid #eee">
        <el-container>
            <div style="width: 100%;position: relative">

                <div style=";margin-bottom: 80px;">
<!--                    <el-input-->
<!--                            placeholder="请输入旅游攻略名"-->
<!--                            clearable-->
<!--                            style="width: 20%; "-->
<!--                            prefix-icon="el-icon-search"-->
<!--                            v-model="Query.title"-->
<!--                    >-->
<!--                    </el-input>-->

                    <el-button type="warning" style="float: right" @click="Up_Add_dialogShow(null)">新增旅游攻略</el-button>
                </div>

                <el-table   border size="medium" current-row-key="id"  :data="dataList" stripe highlight-current-row>
                    <el-table-column prop="type" label="类型" align="center" :formatter="formatType"></el-table-column>

                    <el-table-column prop="title" label="标题" align="center"></el-table-column>
                    <el-table-column prop="description" label="介绍" align="center"></el-table-column>
                    <el-table-column prop="content" label="内容" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="image" label="照片"  align="center">

                        <template slot-scope="{ row }">
                            <img style="width: auto; height: 50px; margin:0  0 0 -40px;position: absolute;
                            border:none;cursor: pointer;"
                                 :preview-src-list=" [`http://localhost:8080/files/downloadFile?name=${row.imageUrl}`] "
                                 :src="getImage(row.imageUrl)"
                            />
                            <div slot="error" class="image-slot">
                                <img  style="width: auto; height: 40px; border:none;" >
                            </div>
                        </template>
                    </el-table-column>


                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary"  size="mini" @click="Up_Add_dialogShow(scope.row)">编辑</el-button>
                            <el-button type="danger" size="mini" @click="DeleteTourism(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--分页组件-->
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>

                <!--2.修改 旅游攻略弹框-->
                <el-dialog title="修改" style="height: 650px"  :visible.sync="dialog_update">
                    <el-form :model="formData_update" :rules="rules" ref="formData_update" label-width="100px" class="demo-formData_update">

                        <el-form-item label="类型：" prop="type">
                            <el-select v-model="formData_update.type" placeholder="请选择类型">
                                <el-option
                                        v-for="option in typeOptions"
                                        :key="option.value"
                                        :label="option.label"
                                        :value="option.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>


                        <el-form-item  label="标题：" prop="title">
                            <el-input v-model="formData_update.title"></el-input>
                        </el-form-item>


                        <el-form-item  label="简介：" prop="description">
                            <el-input v-model="formData_update.description"></el-input>
                        </el-form-item>



                        <el-form-item  label="照片：" prop="imageUrl">

                            <el-upload class="avatar-uploader"
                                       action="http://localhost:8080/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>

                        </el-form-item>

                        <el-form-item label="内容：" prop="content">
                            <el-input type="textarea" v-model="formData_update.content" :rows="10"></el-input>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="UpdateTourism">确定修改</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

                <!--3.新增 旅游攻略弹框-->
                <el-dialog title="新增" style="height: 650px"  :visible.sync="dialog_add">
                    <el-form :model="formData_add" :rules="rules" ref="formData_add" label-width="100px" class="demo-formData_add">
                        <el-form-item label="类型：" prop="type">
                            <el-select v-model="formData_add.type" placeholder="请选择类型">
                                <el-option
                                        v-for="option in typeOptions"
                                        :key="option.value"
                                        :label="option.label"
                                        :value="option.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>


                        <el-form-item  label="标题：" prop="title">
                            <el-input v-model="formData_add.title"></el-input>
                        </el-form-item>


                        <el-form-item  label="简介：" prop="description">
                            <el-input v-model="formData_add.description"></el-input>
                        </el-form-item>

                        <el-form-item  label="照片：" prop="imageUrl">

                            <el-upload class="avatar-uploader"
                                       action="http://localhost:8080/files/uploadFile"
                                       :show-file-list="false"
                                       :on-success="handleAvatarSuccess"
                                       :before-upload="beforeUpload"
                                       ref="upload">
                                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>

                        </el-form-item>

                        <el-form-item label="内容：" prop="content">
                            <el-input type="textarea" v-model="formData_add.content" :rows="10"></el-input>
                        </el-form-item>


                        <el-form-item>
                            <el-button type="primary" @click="AddTourism">确定添加</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

            </div>
        </el-container>
    </el-container>

</div>


<script src="../../../plugins/vue/vue.js"></script>

<!-- 引入element-ui组件库 -->
<script src="../../../plugins/element-ui/index.js"></script>

<!-- 引入axios -->
<script src="../../../plugins/axios/axios.min.js"></script>

<script>
    new Vue({
        el:'#app',    //element
        data() {
            return {
                port: "http://localhost:8080",
                //旅游攻略搜索内容1
                Query: {
                    title: ""
                },
                typeOptions: [
                    { value: 'eat', label: '吃喝' },
                    { value: 'route', label: '线路' },
                    { value: 'stay', label: '文旅' },
                    { value: 'shop', label: '节庆' },
                    { value: 'other', label: '其他' }
                ],

                //显示的数据
                dataList: [],
                //element-ui分页插件
                pagination: {//分页相关模型数据 1
                    currentPage: 1,//当前页码1
                    pageSize: 5,//每页显示的记录数1
                    total: 0,//总记录数1
                },

                //修改弹框内容
                formData_update: {},
                dialog_update: false,


                //新增弹框内容
                formData_add: {},
                dialog_add: false,

                imageUrl: '',
                rules: {
                    type: [
                        { required: true, message: '请输入类型', trigger: 'blur' },
                    ],
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' },
                    ],
                    description: [
                        { required: true, message: '请输入简介', trigger: 'blur' },
                    ],
                    content: [
                        { required: true, message: '请输入内容', trigger: 'blur' },
                    ],

                },
            }
        },
        created() {
            this.getAll();
        },
        methods:{

            getAll(){
                parent ="?title="+this.Query.title;
                axios.get(this.port+"/tourisms/getPageTourism/"+this.pagination.currentPage+"/"+this.pagination.pageSize+parent).then((res)=>{
                    console.log(res)
                    this.dataList=res.data.data.records
                    this.pagination.total=res.data.data.total
                    this.Query.title=""
                })
            },

            //切换页码
            handleCurrentChange(currentPage) {
                console.log("切换页码:")
                this.pagination.currentPage=currentPage;
                console.log(currentPage)
                this.getAll();
            },

            formatType(row) {
                const typeMap = {
                    'eat': '吃喝',
                    'route': '线路',
                    'stay': '文旅',
                    'shop': '节庆',
                    'other': '其他'
                };

                // 如果 row.type 存在于 typeMap 中，返回相应的映射值，否则返回原值
                return typeMap[row.type] || row.type;
            },

            //修改弹框的显示和隐藏
            Up_Add_dialogShow(row){
                //弹框显示时,先清空弹框
                if (row!==null) {
                    //数据回显
                    this.formData_update = row
                    this.imageUrl = `http://localhost:8080/files/downloadFile?name=${row.imageUrl}`

                    //弹框显示
                    this.dialog_update=true
                }else {
                    this.imageUrl = ""
                    this.formData_add={}
                    this.dialog_add=true
                }
            },
            //确定添加
            AddTourism() {
                this.$nextTick(() => {
                    this.$refs.formData_add.validate(valid => {
                        if (valid) {
                            axios.post(this.port+"/tourisms/addTourism", this.formData_add).then((res) => {
                                if (res.data.code) {
                                    this.$message.success(res.data.msg);
                                } else {
                                    this.$message.error(res.data.msg);
                                }
                            }).finally(() => {
                                this.getAll();
                                this.dialog_add = false;
                            });
                        }
                    });
                });
            },


            //确定修改


            UpdateTourism(){
                this.$refs.formData_update.validate(valid => {
                    if (valid) {
                        axios.put(this.port+"/tourisms/updateTourism",this.formData_update).then((res)=>{     //修改数据回显
                            if (res.data.code){
                                // alert("删除成功")
                                this.$message.success(res.data.msg)
                            }else{
                                this.$message.error(res.data.msg)
                            }
                        }).finally(()=>{
                            this.getAll();
                            this.dialog_update=false;
                        })
                    }
                });
            },


            //通过id删除
            DeleteTourism(row){
                this.$confirm("此操作删除当旅游攻略全部信息，是否继续?","提醒",{type: 'info'
                }).then(()=>{
                    axios.delete(this.port+"/tourisms/deleteTourism/"+row.id).then((res)=> {
                        console.log("deleteById:")
                        console.log(res)
                        if (res.data.code){
                            this.$message.success(res.data.msg)
                        }else{
                            this.$message.error(res.data.msg)
                        }
                    }).finally(()=>{
                        this.getAll()
                    })
                }).catch(()=>{
                    //取消删除
                    this.$message.info("取消删除操作")
                })

            },


            center() {
                this.dialog_update = false;
                this.dialog_add = false;

                this.$nextTick(() => {
                    // 清除表单验证状态
                    this.$refs.formData_add && this.$refs.formData_add.clearValidate();
                    this.$refs.formData_update && this.$refs.formData_update.clearValidate();
                });

                this.$message.info("取消操作");
                this.getAll()
            },



            //4.图片文件上传
            handleAvatarSuccess (response, file, fileList) {
                this.formData_add.imageUrl=response.data
                this.formData_update.imageUrl=response.data
                // this.dishAddShow.avatar=<res className="data"></res>;

                this.imageUrl = `http://localhost:8080/files/downloadFile?name=${response.data}`

                console.log(this.imageUrl)
            },
            beforeUpload (file) {
                if(file){
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
            getImage(image) {
                const imageUrl = `http://localhost:8080/files/downloadFile?name=${image}`;
                return imageUrl;
            }
        }
    })

</script>
</body>
</html>
